import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/common/charts_pie.dart';
import 'package:enterprise_v6_app/app/data/entities/double_control/risk_control_analysis_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/double_control/risk_control_assessment_entity.dart';

const _orange = Color(0xFFFC9900);
const _blue = Color(0xFF33A9FF);
const _yellow = Color(0xFFFFCE24);
const _red = Color(0xFFFD6B6B);

class RiskManagementHomeController extends GetxController {
  var initRiskData = <ChartData>[];

  var leftRiskData = <ChartData>[];

  RiskControlAnalysisEntity? analysis;

  int get initRiskCount {
    return initRiskData.fold(0, (previousValue, element) => previousValue + element.y.toInt());
  }

  int get leftRiskCount {
    return leftRiskData.fold(0, (previousValue, element) => previousValue + element.y.toInt());
  }

  var alarmData = <ChartData>[
    ChartData('一级报警', 1, const Color(0xFFFD6B6B)),
    ChartData('二级报警', 1, const Color(0xFFFC9900)),
    ChartData('三级报警', 1, const Color(0xFFFFCE24)),
    ChartData('四级报警', 1, const Color(0xFF33A9FF)),
  ];

  List<String> tableTitle = ['巡检点', '报警', '消警', '误报率'];

  RiskControlAssessmentEntity? assessment;

  Future<RiskControlAnalysisEntity?> getRiskAnalysis() async {
    final resp = await HttpUtil()
        .generateGetRequest<RiskControlAnalysisEntity>(
          Interface.getRiskControlAnalysis,
        )
        .execute();
    resp.onSuccess((result) {
      initRiskData.clear();
      initRiskData.addAll([
        ChartData('低风险', result.data?.initialRiskFour.toDouble() ?? 0, _orange),
        ChartData('一般风险', result.data?.initialRiskOne.toDouble() ?? 0, _red),
        ChartData('较大风险', result.data?.initialRiskThree.toDouble() ?? 0, _blue),
        ChartData('重大风险', result.data?.initialRiskTwo.toDouble() ?? 0, _yellow),
      ]);
      leftRiskData.clear();
      leftRiskData.addAll([
        ChartData('低风险', result.data?.riskFour.toDouble() ?? 0, _orange),
        ChartData('一般风险', result.data?.riskOne.toDouble() ?? 0, _red),
        ChartData('较大风险', result.data?.riskThree.toDouble() ?? 0, _blue),
        ChartData('重大风险', result.data?.riskTwo.toDouble() ?? 0, _yellow),
      ]);
      analysis = result.data;
      update();
    });
    return resp.data;
  }

  Future<RiskControlAssessmentEntity?> getRiskAssessment() async {
    final resp = await HttpUtil()
        .generateGetRequest<RiskControlAssessmentEntity>(
          Interface.getRiskControlAssessment,
        )
        .execute();
    resp.onSuccess((result) {
      assessment = result.data;
      update();
    });
    return resp.data;
  }

  String? get assessmentLevel {
    switch (assessment?.scoreLevel) {
      case 0:
        return '差';
      case 1:
        return '中';
      case 2:
        return '良';
      case 3:
        return '优';
    }
    return null;
  }

  @override
  void onReady() {
    super.onReady();
    getRiskAnalysis();
    getRiskAssessment();
  }
}
